package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.PageList;
import cn.itsource.pethome.Basic.util.UserContext;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.query.PetQuery;
import cn.itsource.pethome.pet.service.IPetService;
import cn.itsource.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 宠物controller类
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    /**
     * 增加宠物
     * @param pet //宠物
     * @return
     */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public AjaxResult save(@RequestBody Pet pet){
        try {
            petService.add(pet);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"增加失败"+e.getMessage());
        }
    }

    /**
     * 删除宠物
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping(value = "{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
    /**
     * 修改宠物
     * @param pet
     * @return
     */
    //@RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Pet pet){
        try {
            petService.update(pet);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }
    /**
     * 查询所有
     * @return
     */
    //@RequestMapping(value = "/findAll",method = RequestMethod.PATCH)
    @PatchMapping
    public PageList<Pet> loadAllByQuery(@RequestBody PetQuery petQuery){
        return petService.loadAllByQuery(petQuery);
    }
    @PatchMapping("/findAll")
    public List<Pet> findAll(){
        return petService.loadAll();
    }
    @PatchMapping("/batchRemove")
    public AjaxResult delete(@RequestBody Long[] ids){
        try {
            petService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
    @PatchMapping("/onState")
    public AjaxResult onState(@RequestBody List<Long> ids){
        try {
            petService.onState(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }
    @PatchMapping("/downState")
    public AjaxResult downState(@RequestBody List<Long> ids){
        try {
            petService.downState(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }
    //===========================================================//
    @PatchMapping("/onSale")
    public PageList<Pet> onSale(@RequestBody PetQuery query){
        return petService.loadAllByQuery(query);
    }
    @PatchMapping("{id}")
    public Pet findPetById(@PathVariable("id") Long id){
        return petService.findById(id);
    }
    @PostMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            //获取登录用户
            User loginUser = UserContext.getLoginUser(request);
            petService.adopt(id,loginUser);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
